Imports System.Xml

Namespace Common.Rss.Version20
    Public Class Item
        Dim _Title As String
        Dim _Link As Uri
        Dim _Description As String
        Dim _Author As String
        Dim _Category As String
        Dim _Comments As Uri
        Dim _PublicationDate As Date
        Dim _Source As Uri
        Dim _SourceName As String

        <ComponentModel.Description("The title of the item.")> _
        Public Property Title() As String
            Get
                Return _Title
            End Get
            Set(ByVal Value As String)
                _Title = Value
            End Set
        End Property

        <ComponentModel.Description("The URL of the item.")> _
        Public Property Link() As Uri
            Get
                Return _Link
            End Get
            Set(ByVal Value As Uri)
                _Link = Value
            End Set
        End Property

        <ComponentModel.Description("The item synopsis.")> _
        Public Property Description() As String
            Get
                Return _Description
            End Get
            Set(ByVal Value As String)
                _Description = Value
            End Set
        End Property
        Public Property SourceName() As String
            Get
                Return _SourceName
            End Get
            Set(ByVal Value As String)
                _SourceName = Value
            End Set
        End Property

        Public Property Author() As String
            Get
                Return _Author
            End Get
            Set(ByVal Value As String)
                _Author = Value
            End Set
        End Property
        Public Property Category() As String
            Get
                Return _Category
            End Get
            Set(ByVal Value As String)
                _Category = Value
            End Set
        End Property
        Public Property Comments() As Uri
            Get
                Return _Comments
            End Get
            Set(ByVal Value As Uri)
                _Comments = Value
            End Set
        End Property

        <ComponentModel.Description("Indicates when the item was published.")> _
        Public Property PublicationDate() As Date
            Get
                Return _PublicationDate
            End Get
            Set(ByVal Value As Date)
                _PublicationDate = Value.ToUniversalTime
            End Set
        End Property
        Public Property Source() As Uri
            Get
                Return _Source
            End Get
            Set(ByVal Value As Uri)
                _Source = Value
            End Set
        End Property
        Sub New()
            _Link = New Uri("http://localhost")
            _Comments = New Uri("http://localhost")
            _Source = New Uri("http://localhost")
            _PublicationDate = Date.Now.ToUniversalTime
        End Sub

        Sub New(ByVal element As XmlElement)
            _Title = element.SelectSingleNode("title").InnerText
            _Link = New Uri(element.SelectSingleNode("link").InnerText)
            _Source = New Uri(element.SelectSingleNode("source").Attributes("url").Value)
            _SourceName = element.SelectSingleNode("source").InnerText
            _Author = element.SelectSingleNode("author").InnerText
            _PublicationDate = DateTime.Parse(element.SelectSingleNode("pubDate").InnerText)
            _Category = element.SelectSingleNode("category").InnerText
            _Description = element.SelectSingleNode("description").InnerText
            _Comments = New Uri(element.SelectSingleNode("comments").InnerText)
        End Sub

        Public Function ToXMLElement(ByVal document As XmlDocument) As XmlElement
            Dim xElement As XmlElement = document.CreateElement("item")
            xElement.AppendChild(CreateXMLElement(document, "title", _Title))
            xElement.AppendChild(CreateXMLElement(document, "link", _Link.ToString))
            xElement.AppendChild(CreateXMLElement(document, "guid", _Link.ToString))
            Dim xSource As XmlElement = CreateXMLElement(document, "source", _SourceName)
            xSource.SetAttribute("url", _Source.ToString)
            xElement.AppendChild(xSource)
            xElement.AppendChild(CreateXMLElement(document, "author", _Author))
            xElement.AppendChild(CreateXMLElement(document, "pubDate", _PublicationDate.ToString("r")))
            xElement.AppendChild(CreateXMLElement(document, "category", _Category))
            xElement.AppendChild(CreateXMLElement(document, "description", _Description))
            xElement.AppendChild(CreateXMLElement(document, "comments", _Comments.ToString))
            Return xElement
        End Function
        Public Overrides Function ToString() As String
            Return Me.ToXMLElement(New XmlDocument()).OuterXml
        End Function

        Private Function CreateXMLElement(ByVal document As XmlDocument, ByVal name As String, ByVal value As String) As XmlElement
            Dim m_Buffer As XmlElement = document.CreateElement(name)
            m_Buffer.InnerText = value
            Return m_Buffer
        End Function

    End Class
End Namespace